package com.itheima.controller;

import com.itheima.entity.Emp;
import com.itheima.entity.EmpQueryParam;
import com.itheima.entity.PageBean;
import com.itheima.entity.Result;
import com.itheima.service.EmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
public class EmpController {
    private static final Logger log = LoggerFactory.getLogger(EmpController.class);

    @Autowired
    private EmpService empService;

    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping("/emps")
    public Result page(EmpQueryParam param) {
        log.info("分页查询：{},{},{},{},{},{}",
                param.getPage(),
                param.getPageSize(),
                param.getName(),
                param.getGender(),
                param.getBegin(),
                param.getEnd());
        PageBean pageBean = empService.page(param);
        return Result.success(pageBean);
    }


    @PostMapping("/emps")
    public Result save(@RequestBody Emp emp) {
        log.info("保存新增员工员工：{}", emp);
        empService.add(emp);
        return Result.success();
    }

    @DeleteMapping("/emps")
    public Result delete(@RequestParam("ids") List<Integer> ids){
        log.info("删除员工，ids: {}", ids);
        empService.delete(ids);
        return Result.success();
    }


    /**
     * 根据 id 查询回显
     */
    @GetMapping("/emps/{id}")
    public Result getById(@PathVariable Integer id) {
        Emp empById = empService.getById(id);
        return Result.success(empById);
    }

    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp) {

        empService.update(emp);
        return Result.success();
    }

    @GetMapping("emps/list")
    public Result getList(Emp emp){
        List<Emp> empList = empService.getList(emp);
        return Result.success(empList);
    }

}